package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"

	hClient "github.com/stellar/go/clients/horizonclient"
	"github.com/stellar/go/keypair"
)

func main() {
	//privKeyStr := "SB6727GOFB7DIFC6ZWLOFAVMQQRUY6KJZWLE42UI5ISQJEBN63HUWZFE"
	publicKeyStr := "GAO7JVRU7JZW3QOW46IIL4ZARW7OF47RRM3HXRGDQ4SKV5FNVD5AEFA7"
	err := CheckBalances(publicKeyStr)
	if err != nil {
		fmt.Println("查询账户余额失败！")
		return
	}
	fmt.Println("\n\n")
	fmt.Println("查询账户余额成功！")
}

func NewAccountNumber() (privKeyStr, publicKeyStr string) {
	//创建私钥公钥
	fmt.Println("-----------创建私钥公钥----------------")
	pair, err := keypair.Random()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("-----------私钥----------------")
	privKeyStr = pair.Seed()
	// SAV76USXIJOBMEQXPANUOQM6F5LIOTLPDIDVRJBFFE2MDJXG24TAPUU7
	fmt.Println("-----------公钥----------------")
	publicKeyStr = pair.Address()
	// GCFXHS4GXL6BVUCXBWXGTITROWLVYXQKQLF4YH5O5JT3YZXCYPAFBJZB
	return privKeyStr, publicKeyStr
}

func CreateTest(publicKeyStr string) bool {
	fmt.Println("-----------创建测试账号----------------")
	// pair 是您之前创建的 keypair 实例
	address := publicKeyStr
	resp, err := http.Get("https://friendbot.stellar.org/?addr=" + address)
	if err != nil {
		log.Fatal(err)
		return false
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
		return false
	}
	fmt.Println(string(body))
	return true
}

// 查询账户余额
func CheckBalances(publicKeyStr string) error {
	fmt.Println("-----------创建测试账号账户余额----------------")
	// Use the default pubnet client
	client := hClient.DefaultTestNetClient

	// Create an account request
	accountRequest := hClient.AccountRequest{AccountID: publicKeyStr}

	// Load the account detail from the network
	account, err := client.AccountDetail(accountRequest)
	if err != nil {
		fmt.Println("+++++获取错误++++++")
		fmt.Println(err)
		return err
	}
	// Account contains information about the stellar account
	fmt.Print(account)
	return nil
}

func MiYaoed25519() {

}
